package my.soft.aiteacher.common.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import my.soft.aiteacher.common.pojo.UserVo;
import my.soft.aiteacher.common.service.IUserService;
import my.soft.aiteacher.utils.R;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Tag(name = "公共-用户管理")
public class UserController {
    @Resource
    private IUserService userService;
    @Operation(summary = "登录")
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public R login(@RequestBody UserVo user){
        String token = userService.login(user);
        return new R(200,"登录成功",token);
    }
    @RequestMapping(value = "/getCaptcha",method = RequestMethod.GET)
    @Operation(summary = "获取验证码")
    public R getCaptcha(){
        return new R(200,"获取验证码成功",userService.getCaptcha());
    }

    @RequestMapping(value = "/sendMail",method = RequestMethod.POST)
    @Operation(summary = "发送登录验证码")
    public R sendMall(@RequestParam String to){
        userService.sendMall(to);
        return R.success("ok");
    }
    @RequestMapping(value = "/mailLogin",method = RequestMethod.POST)
    @Operation(summary = "邮箱登录")
    public R mailLogin(@RequestBody UserVo user){
        String token = userService.mailLogin(user);
        return new R(200,"登录成功",token);
    }
    //通过springsecurity获取用户信息
    @RequestMapping(value = "/userDetail",method = RequestMethod.GET)
    @Operation(summary = "获取用户信息")
    public R userDetail(Authentication authentication){
        UserVo userVo = userService.getUserDetail(authentication.getPrincipal());
        return R.success(userVo);
    }
    @RequestMapping(value = "/updateUserDetail",method = RequestMethod.POST)
    @Operation(summary = "修改用户信息")
    public R updateUserDetail(@RequestBody UserVo userVo){
        userService.updateUserDetail(userVo);
        return R.success("ok");
    }
}
